package com.ruoyi.system.service.module.asset.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.domain.module.asset.EstateAsset;
import com.ruoyi.system.domain.module.asset.EstateAssetExcel;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.excel.PropertyWyfYearTemplateExcel;
import com.ruoyi.system.domain.module.property.PropertyFeeOrderTemplate;
import com.ruoyi.system.mapper.module.asset.EstateAssetMapper;
import com.ruoyi.system.mapper.module.estate.EstateMapper;
import com.ruoyi.system.mapper.system.SysUserMapper;
import com.ruoyi.system.service.module.asset.IEstateAssetService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 资产管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-20
 */
@Service
public class EstateAssetServiceImpl extends ServiceImpl<EstateAssetMapper, EstateAsset> implements IEstateAssetService {

	@Resource
	private EstateMapper estateMapper;
	@Resource
	private SysUserMapper sysUserMapper;
	@Resource
	private EstateAssetMapper estateAssetMapper;
	@Override
	public String importAssetData(List<EstateAssetExcel> templateList, Boolean isUpdateSupport, String operName) {
		if (Validator.isNull(templateList) || templateList.size() == 0) {
			throw new CustomException("导入模板数据不能为空！");
		}
		int ret = 0;
		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		for (EstateAssetExcel template : templateList) {
			try {
				EstateAsset asset = new EstateAsset();
				if (template.getEstateName()==null){
					throw new CustomException("请填写小区！");
				}else {

					QueryWrapper<Estate> qwEstate = new QueryWrapper<>();
					qwEstate.eq("estate_name", template.getEstateName());
					Estate estate = estateMapper.selectOne(qwEstate);
					if (estate == null) {
						throw new CustomException("所属小区不存在！");
					} else {
						asset.setEstateId(estate.getId());
					}
				}

				if (template.getAdminNickName()==null){
					throw new CustomException("请填写负责人！");
				}else {
					QueryWrapper<SysUser> qwSysUser = new QueryWrapper<>();
					qwSysUser.eq("nick_Name", template.getAdminNickName());
					qwSysUser.eq("estate_id",asset.getEstateId());
					SysUser user = sysUserMapper.selectOne(qwSysUser);
					if (user == null) {
						throw new CustomException("该负责人不存在！");
					} else {
						asset.setAdminUserId(Math.toIntExact(user.getUserId()));
					}
				}


				if (StrUtil.isNotEmpty(template.getAssetName())) {
					asset.setAssetName(template.getAssetName());
				}

				if (StrUtil.isNotEmpty(template.getAssetType())) {
					switch (template.getAssetType()) {
						case "固定资产":
							asset.setAssetType(0);
							break;
						case "移动资产":
							asset.setAssetType(1);
							break;
					}
					;
				}

				if (StrUtil.isNotEmpty(template.getState())) {
					switch (template.getState()) {
						case "正常":
							asset.setState(0);
							break;
						case "维修":
							asset.setState(1);
							break;
						case "报废":
							asset.setState(2);
							break;
						case "借出":
							asset.setState(3);
							break;
					}
					;
				}
				if (StrUtil.isNotEmpty(template.getAdminNickName())) {
					asset.setAdminNickName(template.getAdminNickName());
				}

				if (StrUtil.isNotEmpty(template.getDeviceType())) {
					switch (template.getDeviceType()) {
						case "办公设备":
							asset.setDeviceType(0);
							break;
						case "消防设备":
							asset.setDeviceType(1);
							break;
						case "运输设备":
							asset.setDeviceType(2);
							break;
						case "大型设备":
							asset.setDeviceType(3);
							break;
					}
				}

				if (StrUtil.isNotEmpty(template.getDeviceNum())) {
					asset.setDeviceNum(template.getDeviceNum());
				}

				if (template.getProcureMoney()!=null) {
					asset.setProcureMoney(template.getProcureMoney());
				}

				if (StrUtil.isNotEmpty(template.getCycleType())) {
					switch (template.getCycleType()) {
						case "每天":
							asset.setCycleType("day");
							break;
						case "每周":
							asset.setCycleType("week");
							break;
						case "每月":
							asset.setCycleType("month");
							break;
						case "每半年":
							asset.setCycleType("half-year");
							break;
						case "每年":
							asset.setCycleType("year");
							break;
					}
				}

				if (StrUtil.isNotEmpty(template.getCycleDays())) {
					asset.setCycleDays(template.getCycleDays());
				}
				if (StrUtil.isNotEmpty(template.getRemake())) {
					asset.setRemake(template.getRemake());
				}
				if (template.getDepreciationTime()!=null) {
					asset.setDepreciationTime(template.getDepreciationTime());
				}
				if (template.getProcureTime()!=null) {
					asset.setProcureTime(template.getProcureTime());
				}
				if (template.getAssetPosition()!=null) {
					asset.setAssetPosition(template.getAssetPosition());
				}

				ret = estateAssetMapper.insert(asset);


			} catch (Exception e) {
				failureNum++;
				String msg = "<br/>" + failureNum + "、" + template.getAssetName() + " 导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new CustomException(failureMsg.toString());
		} else {
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}
}
